package com.mlethe.android.demo.banner;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.mlethe.android.demo.R;
import com.mlethe.android.demo.banner.adapter.BannerImgAdapter;
import com.mlethe.library.widget.banner.BannerView;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class TestFragment extends Fragment {

    private BannerView bannerView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_banner_test, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        bannerView = getView().findViewById(R.id.main_banner_view);
        BannerImgAdapter adapter = new BannerImgAdapter(new ArrayList<>());
        bannerView.setOnBannerItemListener(new BannerView.OnBannerItemListener() {
            @Override
            public void onClick(int position) {
                Toast.makeText(getContext(), "点击了 position->" + position, Toast.LENGTH_SHORT).show();
            }
        }).setAdapter(adapter);
        Handler handler = new Handler();
        handler.postDelayed(() -> {
            List<String> list = new ArrayList<>();
            list.add("http://pic37.nipic.com/20140113/8800276_184927469000_2.png");
            list.add("http://pic37.nipic.com/20140113/8800276_184927469000_2.png");
            list.add("http://pic37.nipic.com/20140113/8800276_184927469000_2.png");
            list.add("http://pic37.nipic.com/20140113/8800276_184927469000_2.png");
            adapter.addAllItem(list);
            adapter.notifyDataSetChanged();
            handler.postDelayed(() -> {
                adapter.clearItems();
                adapter.notifyDataSetChanged();
            }, 10000);
        }, 3000);
    }

    /*@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (bannerView == null) return;
        if (isVisibleToUser) {
            bannerView.start();
        } else {
            bannerView.stop();
        }
    }*/
}
